package me.zzde.wallpaper;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.widget.TextView;


import me.zzde.wallpaper.Fragment.CategoryFragment;
import me.zzde.wallpaper.Fragment.IndexFragment;
import me.zzde.wallpaper.Fragment.MeFragment;
import me.zzde.wallpaper.utils.ApiServer;

public class MainActivity extends AppCompatActivity {

    private TextView mTextMessage;

    private IndexFragment indexFragment;
    private CategoryFragment categoryFragment;
    private MeFragment meFragment;

    private Fragment[] fragments;
    private int lastShowFragment = 0;

//    private

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    mTextMessage.setText(R.string.title_home);
                    if (lastShowFragment != 0) {
                        switchFrament(lastShowFragment, 0);
                        lastShowFragment = 0;
                    }
                        return true;
                case R.id.navigation_category:
                    mTextMessage.setText(R.string.title_category);
                    if (lastShowFragment != 1) {
                        switchFrament(lastShowFragment, 1);
                        lastShowFragment = 1;
                    }
                    return true;
                case R.id.navigation_me:
                    mTextMessage.setText(R.string.title_me);
                    if (lastShowFragment != 2) {
                        switchFrament(lastShowFragment,   2);
                        lastShowFragment = 2;
                    }
                    return true;
            }
            return false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTextMessage = findViewById(R.id.message);
        BottomNavigationView navigation = findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

        initFragments();


    }


    public void switchFrament(int lastIndex, int index) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.hide(fragments[lastIndex]);
        if (!fragments[index].isAdded()) {
            transaction.add(R.id.fragment_container, fragments[index]);
        }
        transaction.show(fragments[index]).commitAllowingStateLoss();
    }


    private void initFragments() {
        indexFragment = new IndexFragment();
        categoryFragment = new CategoryFragment();
        meFragment = new MeFragment();
        fragments = new Fragment[]{indexFragment, categoryFragment, meFragment};
        lastShowFragment = 0;
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.fragment_container, indexFragment)
                .show(indexFragment)
                .commit();
    }
}
